package com.stylefeng.guns.modular.system.controller.server;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.annotion.BussinessLog;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.OccupationMapper;
import com.stylefeng.guns.common.persistence.model.Occupation;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.OccupationDao;
import com.stylefeng.guns.modular.system.warpper.OccupationWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lmshe
 * @create 2017-11-08 11:03
 * @desc
 */
@Controller
@RequestMapping("/occupation")
public class OccupationController extends BaseController{

    private static String PREFIX = "/system/occupation/";

    @Resource
    private OccupationDao occupationDao;

    @Resource
    private OccupationMapper occupationMapper;

    /**
     * 跳转到职业管理页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "occupation.html";
    }


    /**
     * 跳转到添加职业
     */
    @RequestMapping("/occupation_add")
    public String occupationAdd() {
        return PREFIX + "occupation_add.html";
    }

    /**
     * 跳转到修改职业
     */
    @RequestMapping("/occupation_update/{occupationId}")
    public String occupationUpdate(@PathVariable Integer occupationId, Model model) {

        Occupation occupation = this.occupationMapper.selectById(occupationId);
        model.addAttribute("occupation",occupation);
        LogObjectHolder.me().set(occupation);
        return PREFIX + "occupation_edit.html";
    }
    /**
     * @author lmshe
     * @create 11:20 2017/11/8
     * @desc 查询职业列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String occupation) {
        Page<Occupation> page = new PageFactory<Occupation>().defaultPage();
        List<Map<String, Object>> result = occupationDao.getOccupationList(page, occupation,page.getOrderByField(), page.isAsc());
        page.setRecords((List<Occupation>) new OccupationWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 新增职业
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增职业",key = "occupation")
    public Object add(Occupation occupation) {
        if (ToolUtil.isOneEmpty(occupation, occupation.getOccupation())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        occupation.setCreatetime(new Date());
        occupation.insert();
        return super.SUCCESS_TIP;
    }

    /**
     * 删除职业
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除职业",key = "occupationId")
    public Object delete(@RequestParam Integer occupationId) {
        Occupation occupation = this.occupationMapper.selectById(occupationId);
        occupation.setIsDelete(1);
        occupation.updateById();
        return super.SUCCESS_TIP;
    }
    /**
     * 取消删除职业
     */
    @RequestMapping(value = "/cancel_delete")
    @ResponseBody
    @BussinessLog(value = "取消删除职业",key = "occupationId")
    public Object cancelDelete(@RequestParam Integer occupationId) {
        Occupation occupation = this.occupationMapper.selectById(occupationId);
        occupation.setIsDelete(0);
        occupation.updateById();
        return super.SUCCESS_TIP;
    }
    /**
     * 修改职业
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改职业",key = "occupation")
    public Object update(Occupation occupation) {
        if (ToolUtil.isOneEmpty(occupation, occupation.getId(), occupation.getOccupation())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        this.occupationMapper.updateById(occupation);
        return super.SUCCESS_TIP;
    }
}
